package com.jy.api.controller;

import com.jy.api.config.WebSocketUrlUtils;
import com.jy.api.dao.StoreDao;
import com.jy.api.domain.Store;
import com.jy.api.service.OrderService;
import com.jy.api.service.WechatTemplateService;
import com.jy.api.util.CommonUtil;
import com.jy.api.util.OkHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;

/**
 * Created by hanxi on 2018/4/20.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/pos")
public class PosController {
    private OrderService orderService;

    /**
     * 查询销售额
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{storeId}/sales-total")
    public ResponseEntity getSalesTotal(@PathVariable String storeId,
                                        @RequestParam(name = "productTypeId", defaultValue = "") String typeId,
                                        @RequestParam(name = "productId", defaultValue = "") String productId,
                                        @RequestParam(name = "from") String from,
                                        @RequestParam(name = "to") String to) {
        // 因为pos传递的开始时间与结束时间都没有带时区，因此不能通过spring自动转为date。需要手动指定时区转换
        Date fromDatetime = CommonUtil.parseDate(from);
        Date toDateTime = CommonUtil.parseDate(to);

        return ResponseEntity.ok(orderService.getSalesStatics(storeId, typeId, productId, fromDatetime, toDateTime));
    }

    @Autowired
    private WechatTemplateService wechatTemplateService;
    @Autowired
    private StoreDao storeDao;

    @GetMapping("test")
    public ResponseEntity test(String storeId) throws ParseException {
        Store store = storeDao.findOne(storeId);
        log.info("sendTransactionOverview {}", store);
        if (null != store) {
            wechatTemplateService.sendTransactionOverview(store);
        }
        return ResponseEntity.ok().build();
    }

    private void putWs(Map<String, String> map) {
        try {
            //发送ws
            String url = urlUtils.getWs();
            OkHttpUtils.post(url, com.alibaba.fastjson.JSONObject.toJSONString(map));
        } catch (Exception ex) {
            log.error(" Invitation code initialization failed msg:{}", ex);
        }

    }

    private RestTemplate restTemplate;

    @Autowired
    private WebSocketUrlUtils urlUtils;


    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    private SimpMessagingTemplate simpMessagingTemplate;


    @Autowired
    public void setSimpMessagingTemplate(SimpMessagingTemplate simpMessagingTemplate) {
        this.simpMessagingTemplate = simpMessagingTemplate;
    }


    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }
}
